/*
    Copyright Nicolas Ronvel 2007
	gulix33xp@yahoo.fr

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

using System;
using System.Collections.Generic;
using System.Drawing;

using SdlDotNet.Graphics;
using Sdl = SdlDotNet.Graphics;
using SdlDotNet.Core;
using SdlDotNet.Input;

using BlindShark.GameElements;
using BlindShark.Widgets;

namespace BlindShark.GameStates
{

	public class GS_SelectPlayersKeys : GameState
	{
		#region Variables
		// Surfaces
		Surface _sfFrame;
		
		// Labels
		Label _lblTitle;
		Label[] _lblPlayers;
		Label[] _lblInstructions;
		
		string _sFont;
		
		// Elements Location
		Rectangle _rctFrame;
		
		// Buttons
		Button _btnCancel;
		Button _btnValidate;
		
		// Handling events
		EventHandler<KeyboardEventArgs> _ehKeyPressed;
		List<Key> _lsSelectedKeys;		
		int _iActivePlayer;
		#endregion
		
		public GS_SelectPlayersKeys()
		{	
			_sFont = "fonts/Fertigo_17b.otf";
			
			// Initializing the Labels
			// Title
			_lblTitle = new Label(LanguageManager.Get("SPK_Title"), 36, Color.OrangeRed, TextAlignment.TopMiddle);
			_lblTitle.Underline = true;
			// Players Names
			_lblPlayers = new Label[PlayerManager.Count];
			_lblInstructions = new Label[PlayerManager.Count];
			for(int i=0; i<PlayerManager.Count; i++)
			{
				_lblPlayers[i] = new Label(PlayerManager.GetName(i), 30, Color.LightGray, TextAlignment.TopLeft);
				_lblInstructions[i] = new Label(LanguageManager.Get("SPK_Wait"), 26, Color.LightGray, TextAlignment.TopLeft);
			}
			
			// Initializing the buttons
			// Cancel
			_btnCancel = new Button("gfx/buttons1.jpg", true);
			_btnCancel.Click += new Button.ClickEvent(ToPreviousPlayer);
			_btnCancel.Font = _sFont;
			_btnCancel.TextColor = Color.Orange;
			_btnCancel.TextSize = 20;
			_btnCancel.Text = LanguageManager.Get("SPK_Cancel");
			// Validate
			_btnValidate = new Button("gfx/buttons1.jpg", true);
			_btnValidate.Click += new Button.ClickEvent(Validate);
			_btnValidate.Font = _sFont;
			_btnValidate.TextColor = Color.Orange;
			_btnValidate.TextSize = 20;
			_btnValidate.Text = LanguageManager.Get("SPK_Validate");
			
			// Keyboard Events
			_ehKeyPressed = new EventHandler<KeyboardEventArgs>(KeyPressed);
			Events.KeyboardDown += _ehKeyPressed;
			_lsSelectedKeys = new List<Key>();
		}
		
		public void Draw()
		{	
			ScreenManager.MainScreen.Blit(_sfFrame, _rctFrame.Location);
			
			// Displaying players informations
			for(int i=0; i<PlayerManager.Count; i++)
			{
				ScreenManager.MainScreen.Blit(_lblPlayers[i].Surface, _lblPlayers[i].Location);
				ScreenManager.MainScreen.Blit(_lblInstructions[i].Surface, _lblInstructions[i].Location);
			}
			
			if (_btnCancel.Enabled)
				ScreenManager.MainScreen.Blit(_btnCancel.Surface, _btnCancel.Location);
			if (_btnValidate.Enabled)
				ScreenManager.MainScreen.Blit(_btnValidate.Surface, _btnValidate.Location);
		}
		
		public void CleanUp()
		{
			Events.KeyboardDown -= _ehKeyPressed;
			Events.Remove();
			_btnCancel.CleanUp();
			_btnValidate.CleanUp();
		}
		
		public void HandleEvents()
		{
			while (Events.Poll())
			{
				_btnCancel.HandleEvents();
				_btnValidate.HandleEvents();
			}
		}
		
		public void Init()
		{
			// Initializing the frame
			_sfFrame = new Surface(SurfaceManager.GetSurface("gfx/background_infoWindow.png").Size);
			_sfFrame.Blit(SurfaceManager.GetSurface("gfx/background_infoWindow.png"));
			_rctFrame = new Rectangle(new Point(150,50), _sfFrame.Size);
			
			// Initializing the title
			_lblTitle.Location = new Point(0, 15);
			_lblTitle.Size = _rctFrame.Size;
			_lblTitle.AutoHeight = true;
			_lblTitle.Init();
			_sfFrame.Blit(_lblTitle.Surface, _lblTitle.Location);
			
			// Initializing the Players' informations
			for(int i=0; i<PlayerManager.Count; i++)
			{
				// Names
				_lblPlayers[i].Location = new Point(_rctFrame.X + 20, _rctFrame.Y + 70 + i*100);
				_lblPlayers[i].AutoHeight = true;
				_lblPlayers[i].AutoWidth = true;
				_lblPlayers[i].Init();
				// Instructions
				_lblInstructions[i].Location = new Point(_rctFrame.X + 50, _rctFrame.Y + 110 + i*100);
				_lblInstructions[i].AutoHeight = true;
				_lblInstructions[i].AutoWidth = true;
				_lblInstructions[i].Init();
			}
			
			// Initializing the buttons
			// Cancel
			_btnCancel.Init();
			_btnCancel.Location = new Point(_rctFrame.Right - _btnCancel.Width - 10, _rctFrame.Bottom - _btnCancel.Height - 10);
			_btnCancel.Enabled = false;
			// Validate
			_btnValidate.Init();
			_btnValidate.Location = new Point(_rctFrame.X + 10, _rctFrame.Bottom - _btnValidate.Height - 10);
			_btnValidate.Enabled = false;
			
			_iActivePlayer = 0;
			ToNextPlayer();
		}
		
		public void Update()
		{

		}
		
		public void KeyPressed(object sender, KeyboardEventArgs e)
		{
			// The Return key is not allowed
			if ((e.Key!=Key.Return) && (_iActivePlayer < PlayerManager.Count))
			{
				// Checking that the key has not already been chosen
				if (!_lsSelectedKeys.Contains(e.Key))
				{
					PlayerManager.SetKey(_iActivePlayer, e.Key);
					_lsSelectedKeys.Add(e.Key);
					_iActivePlayer++;
					ToNextPlayer();
				}
			}
		}
		
		#region Events
		private void ToNextPlayer()
		{
			if (_iActivePlayer > 0)
			{
				_lblPlayers[_iActivePlayer - 1].TextColor = Color.Green;
				_lblPlayers[_iActivePlayer - 1].Init();
				_lblInstructions[_iActivePlayer - 1].TextColor = Color.Green;
				_lblInstructions[_iActivePlayer - 1].Text = LanguageManager.Get("SPK_KeySelected");
				_lblInstructions[_iActivePlayer - 1].Init();
			}
			
			if (_iActivePlayer < PlayerManager.Count)
			{
				_lblPlayers[_iActivePlayer].TextColor = Color.Orange;
				_lblPlayers[_iActivePlayer].Init();
				_lblInstructions[_iActivePlayer].TextColor = Color.Orange;
				_lblInstructions[_iActivePlayer].Text = LanguageManager.Get("SPK_ChooseKey");
				_lblInstructions[_iActivePlayer].Init();
			}
			
			if (_iActivePlayer == PlayerManager.Count)
				_btnValidate.Enabled = true;
			if (_iActivePlayer != 0)
				_btnCancel.Enabled = true;
		}
		
		private void ToPreviousPlayer()
		{
			_iActivePlayer--;
			_lsSelectedKeys.RemoveAt(_iActivePlayer);
			
			if (_iActivePlayer >= 0)
			{
				_lblPlayers[_iActivePlayer].TextColor = Color.Orange;
				_lblPlayers[_iActivePlayer].Init();
				_lblInstructions[_iActivePlayer].TextColor = Color.Orange;
				_lblInstructions[_iActivePlayer].Text = LanguageManager.Get("SPK_ChooseKey");
				_lblInstructions[_iActivePlayer].Init();
			}
			
			if (_iActivePlayer < PlayerManager.Count-1)
			{
				_lblPlayers[_iActivePlayer+1].TextColor = Color.LightGray;
				_lblPlayers[_iActivePlayer+1].Init();
				_lblInstructions[_iActivePlayer+1].TextColor = Color.LightGray;
				_lblInstructions[_iActivePlayer+1].Text = LanguageManager.Get("SPK_Wait");
				_lblInstructions[_iActivePlayer+1].Init();
			}
			
			if (_iActivePlayer == 0)
				_btnCancel.Enabled = false;
			_btnValidate.Enabled = false;
		}
		
		private void Validate()
		{
			GameStateManager.ChangeState(new GS_ChangeTrack(true));
		}
		#endregion
	}
}
